﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AttackState : EnemyBaseState
{
    public override void EnterState(Enemy enemy)
    {
        enemy.animState = 2; //遇见敌人的跑动状态
        enemy.targetPoint = enemy.attackList[0];
    }

    public override void OnUpdate(Enemy enemy)
    {

        if(enemy.hasBomb){
            return;
        }
        if(enemy.attackList.Count == 0){    //没人就进入巡逻状态
            enemy.TransitionToState(enemy.patrolState);
        }
        if(enemy.attackList.Count > 1){    //多个 就找一个最近的跑过去
            foreach (var item in enemy.attackList)
            {
                if(Mathf.Abs(enemy.transform.position.x - item.position.x) < Mathf.Abs(enemy.transform.position.x - enemy.targetPoint.position.x)){
                    enemy.targetPoint = item;
                }
            }
        }
        if(enemy.attackList.Count == 1){   //只有一个就朝那个走
            enemy.targetPoint = enemy.attackList[0];
        }

        if(enemy.targetPoint.CompareTag("Player")){ //CompareTag获取当前节点的标签 如果是玩家标签就攻击
            enemy.AttackAction();
        }
        if(enemy.targetPoint.CompareTag("Bomb")){
            enemy.SkillAction();
        }

        enemy.MoveToTarget();
    }
}
